$(document).ready(function () {
  var table = $('table#table').DataTable({
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
        var docnum = $('.search-form').find('input[name="docnum"]').val();
        var material = $('.search-form').find('input[name="material"]').val();
  		var lotnum = $('.search-form').find('input[name="lotnum"]').val();
        var contnum = $('.search-form').find('input[name="contnum"]').val();
        d.docnum = docnum;
        d.material = material;
        d.lotnum = lotnum;
        d.contnum = contnum;
		return d;
      }
    },
    order: [
      [1, 'desc']
    ],
    columns: [{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
      }
     },{
      name: 'documentNumber',
      data: 'documentNumber'
    },{
      name: 'lineNumber',
      data: 'lineNumber'
    },{
      name: 'materialCode',
      data: 'materialCode'
    },{
      name: 'materialName',
      data: 'materialName'
    },{
      name: 'toQty',
      data: 'toQty'
    },{
      name: 'containNumber',
      data: 'containNumber'
    },{
      name: 'lotNumber',
      data: 'lotNumber'
    },{
      name: 'productionDate',
      data: 'productionDate'
    },{
      name: 'expireDate',
      data: 'expireDate'
    },{
      name: 'fromStorageType',
      data: 'fromStorageType'
    },{
      name: 'fromStorageCode',
      data: 'fromStorageCode'
    },{
      name: 'toStorageType',
      data: 'toStorageType'
    },{
      name: 'toStorageLocationCode',
      data: 'toStorageLocationCode'
    },{
      name: 'createdDate',
      data: 'createdDate'
    },{ 
      data: "id",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return ('<div class="btn-group">' +
          '<a  class="btn btn-default btn-xs" title="修改" href="' + baseUrl + '/edit/' + row.id + '"><i class="fa fa-edit"></i></a>' +
          '<a class="btn btn-default btn-xs del" title="删除" href="#"><i class="fa fa-trash-o"></i></a>' +
          '</div>' +
          '&nbsp;&nbsp;');
      }
    }]
  });

  table.on('click', '.del', function () {
    var id = table.row($(this).parents('tr')).data().id;
    $("#delForm #ids").val(id);
    if (id) {
      bootbox.confirm("确定要删除数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    }
  });
  $(".btn.delAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#delForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要删除选中的数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要删除的数据.");
    }
  });

  table.on('click', '.enable', function () {
    var that = this;
    var data = table.row($(this).parents('tr')).data();
    var id = data.id;
    var enabled = data.enabled;
    bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
	if (resul) {
	    $.get(baseUrl + "/enable", {
	      enabled: !enabled,
	      ids: id
	    }, function (res) {
	      if (res) {
	        data.enabled = !enabled;
	        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
	        toastr.info('操作成功!');
	      }
	
	    });
	}});   
  });
  

  //导出 动态组织form提交
  $(".btn.export").on("click", function (e) {
	$("#exportForm .param").remove();
	var $frm = $("#exportForm");
　　　var array = $('.search-form').serializeArray();
　　　for (i = 0, length = array.length; i < length; i++) {
	　　　key = array[i].name;
		value = array[i].value;
		$frm.append($('<input class="param" type="hidden" name = "' + key + '" value = "' + value + '" />')); 
	}
	var params = table.ajax.params();
	$frm.append($('<input class="param" type="hidden" name="input" value = \'' + JSON.stringify(params) + '\' />')); 
	$frm.submit();
	// 阻止默认行为
	e.preventDefault();
	return false;  
  });
  

  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
